package javawork;

import java.util.Scanner;

public class Triangle {
    // 三角形三个顶点的坐标
    private double x1, y1;
    private double x2, y2;
    private double x3, y3;
    
    // 构造方法，初始化三个顶点坐标
    public Triangle(double x1, double y1, double x2, double y2, double x3, double y3) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.x3 = x3;
        this.y3 = y3;
    }
    
    // 计算两点之间的距离（边长）
    private double getDistance(double x1, double y1, double x2, double y2) {
        return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
    }
    
    // 计算三角形面积
    public double calculateArea() {
        // 获取三条边的长度
        double side1 = getDistance(x1, y1, x2, y2);
        double side2 = getDistance(x2, y2, x3, y3);
        double side3 = getDistance(x3, y3, x1, y1);
        
        // 计算半周长
        double s = (side1 + side2 + side3) / 2;
        
        // 应用海伦公式计算面积
        return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
    }
    
    public static void main(String[] args) {
        // 创建Scanner对象读取输入
        Scanner input = new Scanner(System.in);
        
        // 提示用户输入三个点的坐标
        System.out.print("Enter three points for a triangle: ");
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        double x3 = input.nextDouble();
        double y3 = input.nextDouble();
        
        // 创建Triangle对象
        Triangle triangle = new Triangle(x1, y1, x2, y2, x3, y3);
        
        // 计算并输出面积，保留一位小数
        System.out.printf("The area of the triangle is %.1f%n", triangle.calculateArea());
        
        // 关闭输入流
        input.close();
    }
}

    

